package com.bwei.mypaot.base.activity;

import android.app.Dialog;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bwei.mypaot.R;
import com.bwei.mypaot.base.fragment.Fragment_dan;
import com.bwei.mypaot.base.fragment.Fragment_home;
import com.bwei.mypaot.base.fragment.Fragment_work;
import com.bwei.mypaot.base.fragment.ShowFragment;
import com.bwei.mypaot.menu.LoginActivity;
import com.bwei.mypaot.view.Image;
import com.bwei.mypaot.view.MainLinearLayout;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends BaseActivity {

    private FragmentManager manager;
    private ShowFragment showFragment;
    private Fragment_home fragment_home;
    private Fragment_dan fragment_dan;
    private Fragment_work fragment_work;
    //创建集合用来添加fragment
    List<ShowFragment> fragmentList=new ArrayList<ShowFragment>();
    private FragmentTransaction transaction;
    private Button home;
    private Button dan;
    private Button work;
    private Button buy;
    private Button bt_mang;
    private Button bt_song;
    private Image im;
    private MainLinearLayout ll_main;
    private LinearLayout ll_right;
    private SlidingMenu menu;

    private Dialog dialog;
    private Button btn_dialog_cance;
    private Button btn_dialog_photo;
    private Button btn_dialog_cammer;
    private int width;
    private int height;
    final int TAKE_PICTURE = 1;
    private final String IMAGE_TYPE = "image/*";
    private final int IMAGE_CODE = 0;   //这里的IMAGE_CODE是自己任意定义的
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initHeader();
        initWidget();
        setWidgetState();
//        添加侧滑菜单
        menu = getSlidingMenu();
        setBehindContentView(R.layout.activity_right);
        menu.setMode(SlidingMenu.RIGHT);
        menu.setFadeDegree(0.2f);
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        menu.setBehindOffset(150);
         im= (Image) findViewById(R.id.image);
        //处理图片
        Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.mipmap.photo);
        im.setImageBitmap(bitmap);

        Camer();


    }

    @Override
    public void initHeader() {
        inittHeaderWidget();
        setTitle("帮我送");
        addImageLeftListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this,CityActivity.class);
                startActivity(intent);
            }
        },R.mipmap.u45);
        addBtnRightListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this, LoginActivity.class);
                startActivity(intent);
            }
        },"登陆");
    }

    @Override
    public void initWidget() {
        //初始化底部按钮的控件
        home = (Button) findViewById(R.id.home);
        dan = (Button) findViewById(R.id.dan);
        work = (Button) findViewById(R.id.work);
        buy = (Button) findViewById(R.id.buy);
        bt_mang = (Button) findViewById(R.id.bt_mang);
        bt_song = (Button) findViewById(R.id.bt_song);

        manager = getSupportFragmentManager();
        fragment_home = new Fragment_home();
        fragment_dan = new Fragment_dan();
        fragment_work = new Fragment_work();
        addlist();
        addFragmnet(0);

    }
    public void addlist(){
        for (int i=0;i<3;i++) {
            //循环创建五个fragment对象
            showFragment = new ShowFragment();
            switch (i){
                case 0:
                    //把每一个对象作为属性赋给showFragment
                    showFragment.fragment=fragment_home;
                    break;
                case 1:
                    showFragment.fragment=fragment_dan;
                    break;
                case 2:
                    showFragment.fragment=fragment_work;
                    break;

            }
            fragmentList.add(showFragment);

        }
    }
    //循环便利集合讲fragment加入到界面中
    public void addFragmnet(int position){
        transaction = manager.beginTransaction();
        for (int i=0;i<fragmentList.size();i++){
            if (i!=position){
                transaction.hide(fragmentList.get(i).fragment);
            }
        }
        if (fragmentList.get(position).statue==0){
            transaction.add(R.id.fl,fragmentList.get(position).fragment,position+"");
            //在fragment展示以后改变状态
            fragmentList.get(position).statue=1;
            //展示
            transaction.show(fragmentList.get(position).fragment);
        }else{
            transaction.show(fragmentList.get(position).fragment);
        }
        //事物提交
        transaction.commit();
    }

    @Override
    public void setWidgetState() {
        home.setOnClickListener(this);
        dan.setOnClickListener(this);
        work.setOnClickListener(this);
        bt_mang.setOnClickListener(this);
        bt_song.setOnClickListener(this);
        buy.setOnClickListener(this);

        //设置默认点击的模块
        SetSatue(R.id.home);
        //设置默认图片显示
        setTitleStatue(LEFT_IMAGE_RIGHT_TEXT);

    }
    @Override
    public void onClick(View v){
        //根据控件id来调用设置标题和选中状态的方法,并且显示fragment和头布局变化
        switch (v.getId()){
            case R.id.home:
                SetSatue(R.id.home);
                setTitleStatue(LEFT_IMAGE_RIGHT_TEXT);
                addFragmnet(0);
                break;
            case R.id.dan:
                SetSatue(R.id.dan);
                setTitleStatue(LEFT_NO_RIGHT_IMAGE);
                addFragmnet(1);
                break;
            case R.id.work:
                SetSatue(R.id.work);
                setTitleStatue(LEFT_NO_RIGHT_IMAGE);
                addFragmnet(2);
                break;
            case R.id.buy:
                Intent intent=new Intent(MainActivity.this,ThirdActivity.class);
                startActivity(intent);
                break;
            case R.id.bt_mang:
                break;
            case R.id.bt_song:
                break;


        }



    }
    //建立方法设置当点击相应按钮时按钮变为选中状态，并且标题栏随之变化
    public void SetSatue(int id) {
        switch (id) {
            case R.id.home:
                home.setSelected(true);
                dan.setSelected(false);
                work.setSelected(false);
                //重新设置标题
                setTitle("帮我忙");
                break;
            case R.id.dan:
                dan.setSelected(true);
                home.setSelected(false);
                work.setSelected(false);

                setTitle("接单");
                break;
            case R.id.work:
                work.setSelected(true);
                home.setSelected(false);
                dan.setSelected(false);
                setTitle("任务");
                break;

        }
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == TAKE_PICTURE) {
            if (resultCode == RESULT_OK) {
                Bitmap bm = (Bitmap) data.getExtras().get("data");
                im.setImageBitmap(bm);//想图像显示在ImageView视图上，private ImageView img;

            }
        }else if(requestCode == IMAGE_CODE){//相册拍照

            //获得图片的uri
            try {
                ContentResolver resolver =getContentResolver();
                Uri originalUri = data.getData();
                Bitmap bm = MediaStore.Images.Media.getBitmap(resolver, originalUri); //显得到bitmap图片
                if (bm!=null){
                    im.setImageBitmap(bm);
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void Camer(){
        DisplayMetrics metrics=new DisplayMetrics();
        MainActivity.this.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        // 屏幕宽度（像素）
        width = metrics.widthPixels;
        height=metrics.widthPixels;// 屏幕高度（像素）

        // 屏幕高度（像素）
        im.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                dialog = new Dialog(MainActivity.this, R.style.MyDialog);
                View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog_cammer,null);
                dialog.setContentView(view);
                btn_dialog_cammer = (Button) view.findViewById(R.id.btn_dialog_cammer);
                btn_dialog_photo = (Button) view.findViewById(R.id.btn_dialog_photo);
                btn_dialog_cance = (Button) view.findViewById(R.id.btn_dialog_cance);

//                LinearLayout ll_dialog_cammer = (LinearLayout)view.findViewById(R.id.ll_dialog_cammer);
                //设置dialog的位置

                //获得当前窗体
                Window window = dialog.getWindow();

                //重新设置
                WindowManager.LayoutParams lp = window.getAttributes();

                window.setGravity(Gravity.LEFT | Gravity.BOTTOM);
                lp.x = 0; // 新位置X坐标
                lp.y = 0; // 新位置Y坐标
                lp.width = width; // 宽度
                lp.height = (height/10)*3; // 高度

                // dialog.onWindowAttributesChanged(lp);
                //(当Window的Attributes改变时系统会调用此函数，动态设置宽和高
                window.setAttributes(lp);

                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) btn_dialog_cammer.getLayoutParams();
                params.height = height/10;
                params.width = width;
                btn_dialog_cammer.setLayoutParams(params);

                LinearLayout.LayoutParams params1 = (LinearLayout.LayoutParams) btn_dialog_photo.getLayoutParams();
                params1.height = height/10;
                params1.width = width;
                btn_dialog_photo.setLayoutParams(params1);

                LinearLayout.LayoutParams params2 = (LinearLayout.LayoutParams) btn_dialog_cance.getLayoutParams();
                params2.height = height/10;
                params2.width = width;
                btn_dialog_cance.setLayoutParams(params2);
                dialog.show();
//                carmer();
                btn_dialog_cammer.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //拍照
                        startActivityForResult(new Intent("android.media.action.IMAGE_CAPTURE"), TAKE_PICTURE);
                        dialog.dismiss();
                    }
                });
                btn_dialog_photo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        photo();//从相册获取图片
                        dialog.dismiss();

                    }
                });
                btn_dialog_cance.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialog.dismiss();//去掉dialog
                    }
                });

            }
        });



    }
    public void photo(){

        Intent getAlbum = new Intent(Intent.ACTION_GET_CONTENT);
        getAlbum.setType(IMAGE_TYPE);
        startActivityForResult(getAlbum, IMAGE_CODE);
    }


}
